//
//  UrbanPlatesNum.m
//  UrbanManagement
//
//  Created by Can on 18/4/16.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanPlatesNum.h"
#import "ZZZKeyboard.h"

@interface UrbanPlatesNum()
{
    NSInteger _maxNum;
}

@end

@implementation UrbanPlatesNum

- (instancetype)initWithFrame:(CGRect)frame lableName:(NSString *)lableName labelColor:(UIColor *)lableColor text:(NSString *)text placeholder:(NSString *)placeholder buttonTitle:(NSString *)buttonTitle maxNum:(NSInteger)maxNum
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, SCREENW - 30*screenW_6, 50*screenW_6);
        
        [self creatViewWithlableName:lableName labelColor:lableColor text:text placeholder:placeholder buttonTitle:buttonTitle maxNum:maxNum];
        if (maxNum > 0) {
            _maxNum = maxNum;
        }
    }
    return self;
}


-(void)creatViewWithlableName:(NSString *)lableName labelColor:(UIColor *)lableColor text:(NSString *)text placeholder:(NSString *)placeholder buttonTitle:(NSString *)buttonTitle maxNum:(NSInteger)maxNum
{

    //左侧title
    UILabel *lableTitle = [TomamaUI createLabel:CGRectMake(0, 0, 85*screenW_6, self.frame.size.height) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:Color(51, 51, 51) text:lableName];
    [self addSubview:lableTitle];
    lableTitle.center = CGPointMake(lableTitle.center.x, self.frame.size.height/2);
    
    //根据type显示右侧
//    _btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - [TomamaUI getStringWeithtWith:buttonTitle andHeigth:15*screenW_6 andFont:[UIFont systemFontOfSize:15*screenW_6]], 0, [TomamaUI getStringWeithtWith:buttonTitle andHeigth:15*screenW_6 andFont:[UIFont systemFontOfSize:15*screenW_6]], self.frame.size.height) targ:self sel:@selector(takingPictures:) titleColor:Color(21, 121, 242) font:[UIFont systemFontOfSize:15*screenW_6] title:buttonTitle image:nil backGroundImage:nil];
//    [self addSubview:_btn];
//    _btn.center =CGPointMake(_btn.center.x, lableTitle.center.y);
    
    
    ZZZKeyboard *keyboard = [[ZZZKeyboard alloc]initWithFrame:CGRectMake(0, 0, 375, 240)];
    UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lableTitle.frame) + 20*screenW_6, 0, self.frame.size.width - CGRectGetMaxX(lableTitle.frame) - 35*screenW_6, 40*screenW_6)];
    textF.center =CGPointMake(textF.center.x, lableTitle.center.y);
    textF.font = [UIFont systemFontOfSize:15*screenW_6];
    textF.textColor = Color(153, 153, 153);
    textF.text = text;
    textF.placeholder = placeholder;
    
    [textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    textF.inputView = keyboard;
    [self addSubview:textF];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1)];
    lineView.backgroundColor = Color(247, 247, 247);
    [self addSubview:lineView];
}

-(void)textFieldDidChange:(UITextField *)textField
{
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position && _maxNum) {
        if (textField.text.length > _maxNum) {
            textField.text = [textField.text substringToIndex:_maxNum];
        }
    }
    if (_UrbanPlatesNumBlock) {
        _UrbanPlatesNumBlock(textField.text);
    }
}

-(void)takingPictures:(UIButton *)btn
{
    
}


@end
